import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import SvgIcon from '@/components/SvgIcon'
import '@/assets/icon/index.js'
import 'element-ui/lib/theme-chalk/index.css';
import 'element-ui/lib/theme-chalk/base.css';
import Videojs from 'video.js';
import 'video.js/dist/video-js.css';
import "echarts";
import VueECharts from 'vue-echarts';
import moment from 'moment';

Vue.prototype.moment = moment
Vue.prototype.$video = Videojs
Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.component('svg-icon', SvgIcon);
Vue.component('v-chart', VueECharts);

Vue.filter('dateFormat', function (dateStr, pattern='YYYY-MM-DD hh:mm:ss') {
  return moment(dateStr).format(pattern);  
})

router.beforeEach((to, from, next) => {
  if (store.getters['user/token']) { //判断是否有token
    if (to.path === '/login') {
      next({
        path: '/'
      });
    } else {
      if (store.getters['user/currentRoleType'] === undefined) { //判断是否已选择登录角色
        if (to.path !== '/pageFork') {
          next('/pageFork');
        } else {
          next();
        }
      } else if (store.getters['addRouters'].length === 0) { //判断当前用户信息是否拉取
        const roles = store.getters['user/currentRoleType'];
        const currentRoleType = store.getters['user/currentRoleType'];
        store.dispatch('generateRoutes', {
          roles,
          currentRoleType
        }).then(() => {
          const routers = store.getters['addRouters'];
          routers.forEach(element => {
            router.addRoute(element);
          });

          next({
            ...to,
            replace: true
          })
        }).catch(err => {
          console.log(err);
        });
      } else if (from.name === null && to.name === null) { //页面刷新
        const roles = store.getters['user/permissions'];
        const currentRoleType = store.getters['user/currentRoleType'];
        store.dispatch('generateRoutes', {
          roles,
          currentRoleType
        }).then(() => {
          const routers = store.getters['addRouters'];
          routers.forEach(element => {
            router.addRoute(element);
          });

          next({
            ...to,
            replace: true
          })
        }).catch(err => {
          console.log(err);
        });
      } else {
        next();
      }
    }
  } else {
    if (to.path !== '/forgotPassword' && to.path !== '/login') {
      console.log(to.path)
      next('/login'); //重定向到登录页
    } else {
      next(); //放行
    }
  }

})




new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')